<?php
require_once 'role.php';
class advisor extends role{
	public function __construct($id){
		parent::__construct($id,'adv');
	}
	public function request_x($req_id,$version_id,$x){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$text="";
		switch($x){
			case 2:$text="rejected";break;
			case 3:$text="pending_at_pgc";break;
			case 4:$text="pending_at_office";break;
			default:exit();
		}
		$query = "CALL change_status('".$this->getID()."',".$req_id.",".$version_id.",'".$text."')";
		$result = mysqli_query($connection,$query);
	}
	public function get_requests_advisor($flag){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$query="call get_request_status()";
		$result = mysqli_query($connection,$query);
		$a=array("req_id"=>"","version_id"=>"","status"=>"");
		$b=array(array("req_id"=>"","version_id"=>""));		
		
		$i=0;
		$check = 0;
		while($temp = mysqli_fetch_array($result,MYSQLI_ASSOC)){
			/*$tdata['name']=$temp['name'];
			$tdata['req_id']=$temp['req_id'];
			$tdata['item_name']=$temp['item_name'];
			$tdata['item_cost']=$temp['item_cost'];
			$tdata['category']=$temp['category'];
			$tdata['priority']=$temp['priority'];
			$tdata['time_stamp']=$temp['time_stamp'];
			$tdata['advisor']=$temp['advisor'];
			$data[$i]=$tdata;*/
			if($temp['status']==1 && $flag==0){
				
				$a['req_id']=$temp['req_id'];
				$a['version_id']=$temp['version_id'];

			$b[$i]=$a;
			$check++;
			$i++;
			}
			if(($temp['status']==3 || $temp['status']==5) && $flag==1){
				$a['status']=$temp['status'];
				$a['req_id']=$temp['req_id'];
				$a['version_id']=$temp['version_id'];

			$b[$i]=$a;
			$check++;
			$i++;
			}
		}
		mysqli_free_result($result);
		//mysqli_close($connection);
		if($check == 0){
			return null;
		}
		$i=0;
		
		
	$tdata = array("name"=>"","req_id"=>"","item_name"=>"","item_cost"=>"","category"=>"","priority"=>"","time_stamp"=>"",
	"status"=>"");
		$data = array($tdata);
		$j =0;
		foreach($b as $t){
			$query = "call get_request_adv(".$t['req_id'].",".$t['version_id'].")";
			
			$j++;
			$db1 = new DatabaseManager();
		$connection1 = $db1->getDB();
			$result = mysqli_query($connection1,$query);
			if(mysqli_num_rows($result)>=1){
				$temp = mysqli_fetch_array($result,MYSQLI_ASSOC);
				if($temp['adv_id']==$this->getID()){
				$tdata['name']=$temp['name'];
			//	$tdata['balance']=$temp['balance'];
			$tdata['req_id']=$temp['req_id'];
			$tdata['item_name']=$temp['item_name'];
			$tdata['item_cost']=$temp['item_cost'];
			$tdata['category']=ucfirst($temp['category']);
			$tdata['priority']=ucfirst($temp['priority']);
			$tdata['time_stamp']=$temp['time_stamp'];
			switch($t['status']){
				case 1:$tdata['status']="Pending at advisor";break;
				case 2:$tdata['status']="Pending at PGC Chair";break;
				case 3:$tdata['status']="Pending at office";break;
				case 4:$tdata['status']="Modified";break;
				case 5:$tdata['status']="Rejected";break;
				case 6:$tdata['status']="Approved";break;
				default:$tdata['status']="Could not get status";break;
				}
			$data[$i]=$tdata;
			$i++;
			
			}mysqli_free_result($result);}
			//exit();
		}
		if($i==0)
			return NULL;
		return $data;
	}

		public function get_advisor_homepage(){
	$db = new DatabaseManager();
	$connection = $db->getDB();
	$query="call get_advisor_homepage('".$this->getID()."')";
	$result=mysqli_query($connection,$query);
	$temp=mysqli_fetch_array($result,MYSQLI_ASSOC);
	$data=array("name"=>"","email_id"=>"");
	$data['name']=$temp['name'];
	$data['email_id']=$temp['email_id'];
	return $data;
	}
}
	
	
	
	
	
	/*
	
	public function show_advisor_archives($connection){
	$query="call show_advisor_archives('".$this->getID()."')";
	$result=mysqli_query($connection,$query);
	
	$tdata = array("name"=>"","req_id"=>"","item_name"=>"","item_cost"=>"","category"=>"","priority"=>"","time_stamp"=>"","status"=>"");
		$data = array($tdata);
		$i=0;
		while($temp = mysqli_fetch_array($result,MYSQLI_ASSOC)){
			$tdata['name']=$temp['name'];
			$tdata['req_id']=$temp['req_id'];
			$tdata['item_name']=$temp['item_name'];
			$tdata['item_cost']=$temp['item_cost'];
			$tdata['category']=$temp['category'];
			$tdata['priority']=$temp['priority'];
			$tdata['time_stamp']=$temp['time_stamp'];
			$tdata['status']=$temp['status'];
			$data[$i]=$tdata;
			$i++;
		}
		return $data;
	}
	
	public function list_student($connection){
	$query="call list_student('".$this->getID()."')"
;
	$result=mysqli_query($connection,$query);
	$tdata=array(name,email_id);
	$data=array($tdata);
	$i=0;
	
	while($temp=mysqli_fetch_array($result,MYSQLI_ASSOC)){
	$tdata['name']=$temp['name'];
	$tdata['email_id']=$temp['email_id'];
	$data[$i]=$tdata;
	$i++;
	}
	return $data;
	}
	
	
	*/

	
	/*
	public function modifyStatus($req_id,$status,$connection){
		$query = "call changeStatus('".$this->getID()."','".$this->getType()."','".$req_id."','".$status."')";
		$result = mysqli_query($connection,$query);
		mysqli_free_result($result);
	}*/


class pgc extends advisor{
	public function __construct($id){
		parent::__construct($id,'pgc');
	}
	
	public function get_pgc_homepage(){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		//$query = "CALL get_pgc_homepage()";
		$query = "select * from pgc";
		$result = mysqli_query($connection,$query);
		
		if($result){
			if(mysqli_num_rows($result)>=1){
				$temp = mysqli_fetch_array($result,MYSQLI_ASSOC);
				$data = array("name"=>"","email_id"=>"");
				$data['name']=$temp['name'];
				$data['email_id']= $temp['email_id'];
				return $data;
			}
		}
		else return NULL;
	}
	public function show_pgc_archives(){
		$db = new DatabaseManager();
	$connection = $db->getDB();
	$query="call show_pgc_archives('".$this->getID()."')";
	$result=mysqli_query($connection,$query);
	$tdata = array("name"=>"","req_id"=>"","item_name"=>"","item_cost"=>"","category"=>"","priority"=>"","time_stamp"=>"","status"=>"","advisor"=>"");
		$data = array($tdata);
		$i=0;
		while($temp = mysqli_fetch_array($result,MYSQLI_ASSOC)){
			$tdata['name']=$temp['name'];
			$tdata['req_id']=$temp['req_id'];
			$tdata['item_name']=$temp['item_name'];
			$tdata['item_cost']=$temp['item_cost'];
			$tdata['category']=ucfirst($temp['category']);
			$tdata['priority']=ucfirst($temp['priority']);
			$tdata['time_stamp']=$temp['time_stamp'];
			switch($temp['status']){
				case 3:$tdata['status']="Approved";break;
				case 6:$tdata['status']="Rejected";break;
				//default:$tdata['status']="Can't get status";break;
			}
			$tdata['advisor']=$temp['advisor'];
			$data[$i]=$tdata;
			$i++;
		}
		if($i==0)return NULL;
		else
		return $data;
	}
	
	public function get_requests_pgc(){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$query = "CALL get_request_status()";
		$result = mysqli_query($connection,$query);
		$a=array("req_id"=>"","version_id"=>"");
		$b=array(array("req_id"=>"","version_id"=>""));
		$i=0;
		while($temp = mysqli_fetch_array($result,MYSQLI_ASSOC)){
			if($temp['status']==2){	
			$a['req_id']=$temp['req_id'];
			$a['version_id']=$temp['version_id'];
			$b[$i]=$a;
			$i++;
			}
		}
		mysqli_free_result($result);
		//mysqli_close($connection);
		if($i == 0){
			return null;
		}
		$i=0;
			
		$tdata = array("name"=>"","balance"=>"","req_id"=>"","item_name"=>"","item_cost"=>"","category"=>"","priority"=>"","time_stamp"=>"",
		"advisor"=>"");
		$data = array($tdata);
		$db1 = new DatabaseManager();
		$connection1 = $db1->getDB();
		foreach($b as $t){
			echo $query = "call get_request_pgc(".$t['req_id'].",".$t['version_id'].")";
			$result = mysqli_query($connection1,$query);
			if(mysqli_num_rows($result)>=1){
				$temp = mysqli_fetch_array($result,MYSQLI_ASSOC);
				$tdata['name']=$temp['name'];
				$tdata['balance']=$temp['balance'];
				$tdata['req_id']=$temp['req_id'];
				$tdata['item_name']=$temp['item_name'];
				$tdata['item_cost']=$temp['item_cost'];
				$tdata['category']=ucfirst($temp['category']);
				$tdata['priority']=ucfirst($temp['priority']);
				$tdata['time_stamp']=$temp['time_stamp'];
				$tdata['advisor']=$temp['advisor'];
				$data[$i]=$tdata;
				$i++;
			}
			//exit();
		}
		return $data;
	}
	public function request_x($req_id,$version_id,$x){
		$db = new DatabaseManager();
		$connection = $db->getDB();
		$text="";
		switch($x){
			case 2:$text="rejected";break;
			case 3:$text="pending_at_office";break;
			default:exit();
		}
		$query = "CALL change_status('".$this->getID()."',".$req_id.",".$version_id.",'".$text."')";
		$result = mysqli_query($connection,$query);
		
	}
}

?>